package com.emcom.gotour.mvc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.emcom.gotour.enums.PagingAction;
import com.emcom.gotour.mvc.model.TourContentModel;
import com.emcom.gotour.service.TourCategoryService;
import com.emcom.gotour.service.TourManageService;
import com.emcom.gotour.utils.Paging;
import com.emcom.gotour.utils.PagingUtils;
import com.emcom.gotour.vo.TourContentVO;

@Controller
public class DeleteTourController {

	@Autowired
	private MessageSource messageSource;

	@Autowired
	private TourManageService tourManageService;

	@Autowired
	private TourCategoryService tourCategoryService;

	@RequestMapping(value={"/admin/delete_tour_action.html"})
	public String deleteTourAction(HttpServletRequest request) {
		String id = request.getParameter("id");
		tourManageService.deleteTour(Long.valueOf(id));
		Paging paging = PagingUtils.loadPaging(request, TourContentModel.PAGING_NAME);
		if(paging == null) {
			PagingUtils.forceInitPaging(request, tourManageService.queryMultiContent(null, null, null), PagingUtils.PAGE_SIZE, TourContentModel.PAGING_NAME);
			paging = PagingUtils.loadPaging(request, TourContentModel.PAGING_NAME);
		}
		String page = String.valueOf(paging.getCurrentPage());
		String action = PagingAction.TO.name();
		PagingUtils.forceInitPaging(request, tourManageService.queryMultiContent(null, null, null), paging.getPageSize(), TourContentModel.PAGING_NAME);
		paging = PagingUtils.loadPaging(request, TourContentModel.PAGING_NAME);
		paging = PagingUtils.paging(PagingAction.valueOf(action), paging, Integer.valueOf(page));
		PagingUtils.savePaging(request, paging, TourContentModel.PAGING_NAME);
		List<TourContentVO> tourContentVOList = TourContentModel.buildTourContentVOList(paging.getPagingList());
		TourContentModel.getView(request, messageSource, tourCategoryService, tourContentVOList);
		request.setAttribute("page", paging.getCurrentPage());
		request.setAttribute("total", paging.getLastPage());
		request.setAttribute("pageSize", paging.getPageSize());
		return "/admin/tour";
	}

}
